package com.aysf.drafter.controller;

import com.aysf.drafter.entity.BedLayout;
import com.aysf.drafter.entity.Result;
import com.aysf.drafter.service.IBedLayoutService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author MaoZz
 * @since 2024-12-21 17:19:15
 */
@RestController
@CrossOrigin
@RequestMapping("/bedLayout")
public class BedLayoutController {

    @Autowired
    private IBedLayoutService bedLayoutService;


    @RequestMapping("/getAll")
    public Result getAll1() {
        List<BedLayout> list = bedLayoutService.list();
        if (list.size() > 0) {
            return new Result("success", list);
        } else return new Result("error", null);
    }


    @RequestMapping("/getall")
    public Result getAll(String floor, String bedStatus, String key) {
        QueryWrapper<BedLayout> wrapper = new QueryWrapper<BedLayout>()
                .like("floor", floor)
                .like("bed_status", bedStatus)
                .like("room_number", key);
        List<BedLayout> list = bedLayoutService.list(wrapper);
        if (list.size() > 0) {
            return new Result("success", list);

        } else return new Result("error", null);
    }


    @RequestMapping("/save")
    public Result save(@RequestBody BedLayout bedLayout) {
        boolean save = bedLayoutService.save(bedLayout);
        if(save){return  new Result("success", null);}
        else return new Result("error", null);
    }


    @RequestMapping("/del")
    public Result del(String roomNumber) {
        boolean flag = bedLayoutService.remove(new QueryWrapper<BedLayout>()
                .eq("room_number", roomNumber));
        if(flag){return  new Result("success", null);}
        else return new Result("error", null);
    }



    @RequestMapping("/gettt")
    public Result gettt() {
        HashMap<String, List<BedLayout>> map = new HashMap<>();
        List<BedLayout> list = bedLayoutService.list();
        List<BedLayout> list1 = bedLayoutService.list(new QueryWrapper<BedLayout>()
                .eq("bed_status", "空床"));
        map.put("all",list);
        map.put("free",list1);
        if(map.size()>0){
            return new Result("success", map);
        }else return new Result("error", null);
    }


    }
